package com.Akronmcp.hta.main;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.Akronmcp.hta.obj.HManager;
import com.Akronmcp.hta.obj.HopBar;
import com.Akronmcp.hta.obj.HopBook;
import com.Akronmcp.hta.obj.HopChat;
import com.Akronmcp.hta.obj.HopHolo;
import com.Akronmcp.hta.obj.HopTitle;
import com.Akxymcp.hta.command.ComPath;
import com.Akxymcp.hta.command.ValuePath;

/**指令处理类*/
public class HComer implements CommandExecutor,TabCompleter {

	static HManager hta = HM.hta;
	static FileConfiguration lang = HM.lang;
	static boolean papiloaded = HM.papiloaded;

	@Override
    public boolean onCommand(CommandSender sd, Command cmd, String label, String[] args)
    {
        
//		HopHolo ho = new HopHolo(hta);
//    	ho.setText("2333333sdk{player}");
//    	ho.setPapi(true);
//    	ho.setModule("the7");
//    	
//    	List<String> l = new ArrayList<String>();
//    	List<String> l2 = new ArrayList<String>();
//
//    	l.add("===");
//    	l.add("======");
//
//    	l2.add("======");
//    	l2.add("===");
//    	
//    	ho.setPreList(l);
//    	ho.setSufList(l2);
//    	
//    	ho.setLocation(((Player)sd).getLocation());
//    	
//    	ho.create();
//    	
//    	System.out.println("boynextdoor");
    	
    	
       if(cmd.getName().equalsIgnoreCase("htab"))
       {
    	  return htab.input(sd, args);
       }
       //书籍指令
       
       if(cmd.getName().equalsIgnoreCase("htas"))
       {
    	  return htas.input(sd, args);
       }
       //即时生成指令
       
       if(cmd.getName().equalsIgnoreCase("hta"))
       {//主指令
    	   return htam.input(sd, args);
       }
       
       return false;
    }

	
	static
	{
		initHTA();
		initHTAB();
		initHTAS();
	}
	
	static ComPath htas;
	public static void initHTAS()
	{
		htas = new ComPath("htas");
		
		htas.setComfun((sd,v)->{
			if(sd instanceof Player)
			{
				Player p = (Player)sd;
				hta.sendTellraw("help3",p);
			}else
			{
				sd.sendMessage(help("help3"));
			}
			return true;
		});
		
		htas.addPath(new ComPath("hchat")
			.addValuePath((ValuePath) new ValuePath("[text]")
				.setComfun((sd,v)->
				{
					v.set(0,v.get(0).replace("_"," "));
					if(sd instanceof Player)
					{
						Player p = (Player)sd;
						HopChat.sendBy(p,v.get(0),papiloaded);
					}
					return true;
				})
				.addValuePath((ValuePath) new ValuePath("[p]")
					.setComfun((sd,v)->
					{
						v.set(0,v.get(0).replace("_"," "));
						if(v.get(1).equals("*"))
						{
							HopChat.sendtoallBy(v.get(0),papiloaded);
							return true;
						}
						
						Player p = Bukkit.getPlayer(v.get(1));
						if(p==null||!p.isOnline())
						{
							sd.sendMessage("玩家不在线:"+v.get(1));
							return true;	
						}
						HopChat.sendBy(p,v.get(0),papiloaded);
						return true;
					}))));
		
		htas.addPath(new ComPath("htitle")
			.addValuePath((ValuePath) new ValuePath("[text]")
				.setComfun((sd,v)->
				{
					v.set(0,v.get(0).replace("_"," "));
					if(sd instanceof Player)
					{
						Player p = (Player)sd;
						HopTitle.sendBy(p,v.get(0),"TITLE",null,papiloaded);
					}
					return true;
				})
				.addValuePath((ValuePath) new ValuePath("[p]")
					.setComfun((sd,v)->
					{
						v.set(0,v.get(0).replace("_"," "));
						if(v.get(1).equals("*"))
						{
							HopTitle.sendtoallBy(v.get(0),"TITLE",null,papiloaded);
							return true;
						}
						
						Player p = Bukkit.getPlayer(v.get(1));
						if(p==null||!p.isOnline())
						{
							sd.sendMessage("玩家不在线:"+v.get(1));
							return true;	
						}
						HopTitle.sendBy(p,v.get(0),"TITLE",null,papiloaded);
						return true;
					})
					.addValuePath((ValuePath) new ValuePath("[mo]")
						.setSourceGetor((sd)->new ArrayList<>(HManager.htmlist.keySet()))
						.setComfun((sd,v)->
						{
							v.set(0,v.get(0).replace("_"," "));
							if(v.get(1).equals("*"))
							{
								HopTitle.sendtoallBy(v.get(0),"TITLE",v.get(2),papiloaded);
								return true;
							}
							
							Player p = Bukkit.getPlayer(v.get(1));
							if(p==null||!p.isOnline())
							{
								sd.sendMessage("玩家不在线:"+v.get(1));
								return true;	
							}
							HopTitle.sendBy(p,v.get(0),"TITLE",v.get(2),papiloaded);
							return true;
						})
						.addValuePath((ValuePath) new ValuePath("[type]")
							.setSourceGetor((sd)->
							{
								return Arrays.asList("actionbar","title","subtitle");
							})
							.setComfun((sd,v)->
							{
								v.set(0,v.get(0).replace("_"," "));
								if(v.get(1).equals("*"))
								{
									HopTitle.sendtoallBy(v.get(0),v.get(3),v.get(2),papiloaded);
									return true;
								}
								Player p = Bukkit.getPlayer(v.get(1));
								if(p==null||!p.isOnline())
								{
									sd.sendMessage("玩家不在线:"+v.get(1));
									return true;	
								}
								HopTitle.sendBy(p,v.get(0),v.get(3),v.get(2),papiloaded);
								return true;
							}))))));
		
		
		htas.addPath(new ComPath("hbar")
			.addValuePath((ValuePath) new ValuePath("[text]")
				.setComfun((sd,v)->
				{
					v.set(0,v.get(0).replace("_"," "));
					if(sd instanceof Player)
					{
						Player p = (Player)sd;
						HopBar.sendBy(p,v.get(0),null,papiloaded);
					}
					return true;
				})
				.addValuePath((ValuePath) new ValuePath("[p]")
					.setComfun((sd,v)->
					{
						v.set(0,v.get(0).replace("_"," "));
						if(v.get(1).equals("*"))
						{
							HopBar.sendtoallBy(v.get(0),null,papiloaded);
							return true;
						}
						Player p = Bukkit.getPlayer(v.get(1));
						if(p==null||!p.isOnline())
						{
							sd.sendMessage("玩家不在线:"+v.get(1));
							return true;	
						}
						HopBar.sendBy(p,v.get(0),null,papiloaded);
						return true;
					})
					.addValuePath((ValuePath) new ValuePath("[mo]")
						.setSourceGetor((sd)->new ArrayList<>(HManager.htmlist.keySet()))
						.setComfun((sd,v)->
						{
							v.set(0,v.get(0).replace("_"," "));
							if(v.get(1).equals("*"))
							{
								HopBar.sendtoallBy(v.get(0),v.get(2),papiloaded);
								return true;
							}
							Player p = Bukkit.getPlayer(v.get(1));
							if(p==null||!p.isOnline())
							{
								sd.sendMessage("玩家不在线:"+v.get(1));
								return true;	
							}
							HopBar.sendBy(p,v.get(0),v.get(2),papiloaded);
							return true;
						})))));
		
		
		htas.addPath(new ComPath("hbook")
			.addValuePath((ValuePath) new ValuePath("[text]")
				.setComfun((sd,v)->
				{
					v.set(0,v.get(0).replace("_"," "));
					if(sd instanceof Player)
					{
						Player p = (Player)sd;
						HopBook.sendBy(p,v.get(0),papiloaded);
					}
					return true;
				})
				.addValuePath((ValuePath) new ValuePath("[p]")
					.setComfun((sd,v)->
					{
						v.set(0,v.get(0).replace("_"," "));
						if(v.get(1).equals("*"))
						{
							HopBook.sendtoallBy(v.get(0),papiloaded);
							return true;
						}
						Player p = Bukkit.getPlayer(v.get(1));
						if(p==null||!p.isOnline())
						{
							sd.sendMessage("玩家不在线:"+v.get(1));
							return true;	
						}
						HopBook.sendBy(p,v.get(0),papiloaded);
						return true;
					}))));
		
	}
	
	static ComPath htab;
	public static void initHTAB()
	{
		htab = new ComPath("htab");
		
		htab.setComfun((sd,v)->{
			if(sd instanceof Player)
			{
				Player p = (Player)sd;
				hta.sendTellraw("help2",p);
			}else
			{
				sd.sendMessage(help("help2"));
			}
			return true;
		}).setInGame(true).setPerm("hta.book");
		
		htab.addPath(new ComPath("nextpage")
			.setComfun((sd,v)->
			{
				Player p = (Player) sd;
				ItemStack bo = p.getInventory().getItemInMainHand();
				String id = HopBook.getID(bo);
				if(id==null)
				{
					sd.sendMessage(help("needbookon"));
					return true;
				}
				
				hta.getHopBook(id).nextPage(p);
				return true;
			}));
		
		htab.addPath(new ComPath("lastpage")
			.setComfun((sd,v)->
			{
				Player p = (Player) sd;
				ItemStack bo = p.getInventory().getItemInMainHand();
				String id = HopBook.getID(bo);
				if(id==null)
				{
					sd.sendMessage(help("needbookon"));
					return true;
				}
				
				hta.getHopBook(id).lastPage(p);
				return true;
			}));
		
		htab.addPath(new ComPath("tomark")
			.setComfun((sd,v)->
			{
				Player p = (Player) sd;
				ItemStack bo = p.getInventory().getItemInMainHand();
				String id = HopBook.getID(bo);
				if(id==null)
				{
					sd.sendMessage(help("needbookon"));
					return true;
				}
				
				hta.getHopBook(id).toMarkPage(p);
				return true;
			}));
		
		htab.addPath(new ComPath("topage")
			.addValuePath((ValuePath) new ValuePath("[page]")
				.setComfun((sd,v)->
				{
					Player p = (Player) sd;
					ItemStack bo = p.getInventory().getItemInMainHand();
					String id = HopBook.getID(bo);
					if(id==null)
					{
						sd.sendMessage(help("needbookon"));
						return true;
					}
					
					try {
						int pg = Integer.valueOf(v.get(0));
						hta.getHopBook(id).openPage(p,pg);
					}catch (Exception e) {
						hta.getHopBook(id).openPage(p,v.get(0));
					}
					
					return true;
				})));		
		
	}
	
	static ComPath htam;
	public static void initHTA()
	{
		htam = new ComPath("hta");
		
		htam.setComfun((sd,v)->{
			if(sd instanceof Player)
			{
				Player p = (Player)sd;
				hta.sendTellraw("help1",p);
			}else
			{
				sd.sendMessage(help("help1"));
			}
			return true;
		});
		
		htam.addPath(new ComPath("reload")
			.setComfun((sd,v)->
			{
				try {
					HLoader.reload_conf(hta);
					sd.sendMessage(help("loadok"));
				} catch (Exception e) {
					e.printStackTrace();
					sd.sendMessage(help("loadnotok"));
				}
				return true;
			}));
		
		htam.addPath(new ComPath("echo")
			.addValuePath((ValuePath) new ValuePath("[htxid]")
				.setSourceGetor((sd)->
				{
					return new ArrayList<String>(HManager.hchatMap.keySet());
				})
				.setComfun((sd,v)->
				{
					HopChat htx = hta.getHopChat(v.get(0).toString());
					if(htx==null)
					{
						sd.sendMessage("未找到hopchat:"+v.get(0));
						return true;
					}
					if(sd instanceof Player)
					htx.send((Player) sd);
					
					return true;
				})
				.addValuePath((ValuePath) new ValuePath("[p]")
					.setComfun((sd,v)->{
						HopChat htx = hta.getHopChat(v.get(0));
						if(htx==null)
						{
							sd.sendMessage("未找到hopchat:"+v.get(0));
							return true;
						}
						
						if(v.get(1).equals("*"))
						{
							htx.sendToAll();
							return true;
						}
						
						Player p = Bukkit.getPlayer(v.get(1));
						if(p==null||!p.isOnline())
						{
							sd.sendMessage("玩家不在线:"+v.get(1));
							return true;
						}
						
						htx.send(p);
						return true;
					}))));
		
		htam.addPath(new ComPath("echotitle")
				.addValuePath((ValuePath) new ValuePath("[htxid]")
					.setSourceGetor((sd)->
					{
						return new ArrayList<String>(HManager.htitleMap.keySet());
					})
					.setComfun((sd,v)->
					{
						HopTitle htx = hta.getHopTitle(v.get(0));
						if(htx==null)
						{
							sd.sendMessage("未找到hoptitle:"+v.get(0));
							return true;
						}
						if(sd instanceof Player)
						htx.send((Player) sd);
						
						return true;
					})
					.addValuePath((ValuePath) new ValuePath("[p]")
						.setComfun((sd,v)->{
							HopTitle htx = hta.getHopTitle(v.get(0));
							if(htx==null)
							{
								sd.sendMessage("未找到hoptitle:"+v.get(0));
								return true;
							}
							
							if(v.get(1).equals("*"))
							{
								htx.sendToAll();
								return true;
							}
							
							Player p = Bukkit.getPlayer(v.get(1));
							if(p==null||!p.isOnline())
							{
								sd.sendMessage("玩家不在线:"+v.get(1));
								return true;
							}
							
							htx.send(p);
							return true;
						}))));
		
		htam.addPath(new ComPath("echobar")
				.addValuePath((ValuePath) new ValuePath("[htxid]")
					.setSourceGetor((sd)->
					{
						return new ArrayList<String>(HManager.hbarMap.keySet());
					})
					.setComfun((sd,v)->
					{
						HopBar htx = hta.getHopBar(v.get(0));
						if(htx==null)
						{
							sd.sendMessage("未找到hopbar:"+v.get(0));
							return true;
						}
						if(sd instanceof Player)
						htx.send((Player) sd);
						
						return true;
					})
					.addValuePath((ValuePath) new ValuePath("[p]")
						.setComfun((sd,v)->{
							HopBar htx = hta.getHopBar(v.get(0));
							if(htx==null)
							{
								sd.sendMessage("未找到hopbar:"+v.get(0));
								return true;
							}
							
							if(v.get(1).equals("*"))
							{
								htx.sendToAll();
								return true;
							}
							
							Player p = Bukkit.getPlayer(v.get(1));
							if(p==null||!p.isOnline())
							{
								sd.sendMessage("玩家不在线:"+v.get(1));
								return true;
							}
							
							htx.send(p);
							return true;
						}))));
		
		htam.addPath(new ComPath("givebook")
				.addValuePath((ValuePath) new ValuePath("[bid]")
					.setSourceGetor((sd)->
					{
						return new ArrayList<String>(HManager.hbookMap.keySet());
					})
					.setComfun((sd,v)->
					{
						HopBook htx = hta.getHopBook(v.get(0));
						if(htx==null)
						{
							sd.sendMessage("未找到hopbook:"+v.get(0));
							return true;
						}
						if(sd instanceof Player)
						htx.give((Player) sd);
						
						return true;
					})
					.addValuePath((ValuePath) new ValuePath("[p]")
						.setComfun((sd,v)->{
							HopBook htx = hta.getHopBook(v.get(0));
							if(htx==null)
							{
								sd.sendMessage("未找到hopbook:"+v.get(0));
								return true;
							}
							
							if(v.get(1).equals("*"))
							{
								htx.giveToAll();
								return true;
							}
							
							Player p = Bukkit.getPlayer(v.get(1));
							if(p==null||!p.isOnline())
							{
								sd.sendMessage("玩家不在线:"+v.get(1));
								return true;
							}
							
							htx.give(p);
							return true;
						}))));
		
		htam.addPath(new ComPath("tobookpage")
				.addValuePath((ValuePath) new ValuePath("[htxid]")
					.setSourceGetor((sd)->
					{
						return new ArrayList<String>(HManager.hchatMap.keySet());
					})
					.setComfun((sd,v)->
					{
						HopChat htx = hta.getHopChat(v.get(0));
						if(htx==null)
						{
							sd.sendMessage("未找到hopchat:"+v.get(0));
							return true;
						}
						if(sd instanceof Player)
						HopBook.openToPage((Player) sd,htx,true);
						
						return true;
					})
					.addValuePath((ValuePath) new ValuePath("[p]")
						.setComfun((sd,v)->{
							HopChat htx = hta.getHopChat(v.get(0));
							if(htx==null)
							{
								sd.sendMessage("未找到hopchat:"+v.get(0));
								return true;
							}
							
							if(v.get(1).equals("*"))
							{
								for(Player p0:Bukkit.getOnlinePlayers())
								HopBook.openToPage(p0,htx,true);
								
								return true;
							}
							
							Player p = Bukkit.getPlayer(v.get(1));
							if(p==null||!p.isOnline())
							{
								sd.sendMessage("玩家不在线:"+v.get(1));
								return true;
							}
							
							HopBook.openToPage(p,htx,true);
							return true;
						}))));
		
		htam.addPath(new ComPath("tpholo")
				.addValuePath((ValuePath) new ValuePath("[htxid]")
					.setSourceGetor((sd)->
					{
						return new ArrayList<String>(HManager.hholoMap.keySet());
					})
					.setComfun((sd,v)->
					{
						HopHolo htx = hta.getHopHolo(v.get(0));
						if(htx==null)
						{
							sd.sendMessage("未找到hopholo:"+v.get(0));
							return true;
						}
						if(sd instanceof Player)
						((Player) sd).teleport(htx.getLocation());
						
						return true;
					})));
		
		
		
	}
	
	/**发送帮助信息*/
	public static String help(String path)
	{
		String str = null;
		if(!path.contains("help"))
		{
			str = lang.getString("prefix","");
		}
		
		str+= lang.getString(path,path);
		str = str.replace("&","§");
		return str;
	}

	@Override
	public List<String> onTabComplete(CommandSender sd, Command cmd, String alias, String[] args) {
		
		if(cmd.getName().equalsIgnoreCase("htab"))
			return htab.tabInput(sd, args);
		if(cmd.getName().equalsIgnoreCase("htas"))
			return htas.tabInput(sd, args);
		if(cmd.getName().equalsIgnoreCase("hta"))
			return htam.tabInput(sd, args);
		
		return null;
	}
	
	
	
}
